package com.ltsh.fim.client;


import com.alibaba.fastjson.JSONObject;
import com.ltsh.fim.client.model.LoginReq;
import com.ltsh.fim.client.model.ThreadParam;
import com.ltsh.fim.common.inf.ResultCallback;
import com.ltsh.fim.common.model.FimRequest;

import java.io.UnsupportedEncodingException;


/**
 * Created by root on 1/11/18.
 */
public class FimClient {
    private static ThreadLocal<ThreadParam> threadParamThreadLocal=new ThreadLocal<ThreadParam>(){
        @Override
        protected ThreadParam initialValue() {
            ThreadParam param=new ThreadParam();
            param.setThreadId(Thread.currentThread().getId());
            return param;
        }
    };
    public void run(int port) {
        FimClientContext clientContext = new DefaultFimClientContext(7070, "192.168.22.224", port);
//        clientContext.sendMsg("123", "123");

        FimRequest request = new FimRequest();
        request.setType((byte)1);
        request.setId(Long.parseLong(threadParamThreadLocal.get().getRequestId()));
        try {
            LoginReq loginReq = new LoginReq();
            loginReq.setLoginName("test");
            loginReq.setPassword("123456");
            request.setData(JSONObject.toJSONString(loginReq).getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        clientContext.sendMsg(request, new ResultCallback() {
            @Override
            public void callback(Object result) {
                System.out.println("callback");
            }
        });
    }

    public static void main(String[] args) {
        new FimClient().run(8080);
    }

}